/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: LoadingState.java 44 2008-05-14 18:22:13Z jens464 $
*/
package albareth.frontend.slick.state;

import java.io.IOException;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.loading.DeferredResource;
import org.newdawn.slick.loading.LoadingList;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;
import albareth.frontend.slick.ui.UIToolkit;

public class LoadingState extends BasicGameState
{
    /** The next resource to load. */
    private DeferredResource nextResource;
    /** True if we've loaded all the resources and started rendering. */
    private boolean ready = false;

    private boolean frameRendered = false;



    public int getID()
    {
        return SlickFrontend.STATE_LOADING;
    }

    public void init(GameContainer container, StateBasedGame game)
            throws SlickException
    {
    }

    
    public void enter(GameContainer container, StateBasedGame game)
            throws SlickException
    {
    }

    public void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException
    {
        int total = LoadingList.get().getTotalResources();
        int loaded = LoadingList.get().getTotalResources() - LoadingList.get().getRemainingResources();
        float pct = (float)loaded / (float)total;
        //System.err.println(pct);

        
        UIToolkit.drawBar(300, 280,
                SlickResources.CASTING_BAR_FULL,
                SlickResources.CASTING_BAR_EMPTY, pct
        );

        g.setFont(SlickResources.SMALL_FONT);
        g.drawString(
                "Loading",
                300 + UIToolkit.getCenterStringX(
                        "Loading", SlickResources.CASTING_BAR_EMPTY.getWidth(), SlickResources.SMALL_FONT),
                280 + 4
        );
        
        frameRendered = true;
    }

    public void update(GameContainer container, StateBasedGame game, int delta)
            throws SlickException
    {
        if (nextResource != null) {
            try {
                nextResource.load();
            } catch (IOException e) {
                throw new SlickException("Failed to load: "+nextResource.getDescription(), e);
            }
            nextResource = null;
        }
        
        if (frameRendered == true && ready == false)
        {
            if (LoadingList.get().getRemainingResources() > 0) {
                nextResource = LoadingList.get().getNext();
                frameRendered = false;
            } else {
                ready = true;
            }
        }
        
        //auto start of game:
        if (ready) {
            LoadingList.setDeferredLoading(false);
            //SlickResources.loadSheets();
            //SlickResources.loadMouseCursors();
            game.enterState(SlickFrontend.STATE_TITLE);
        }

    }

}
